
local cjson = require("cjson")
math.randomseed(os.time())
cjson.encode_empty_table_as_object(false)

local req = {}

math.randomseed(os.time())
local counter = 1
local reqs = 0
local threads = {}

wrk.model = {
   url = "http://localhost/",
   timeout = 30,
   phase = {
      {connections="1", duration="2m"},
      -- {connections="1", duration="4"},
      -- {connections="7", duration="10s"},
      -- {connections="10", duration="5s"},
      -- {connections="1", duration="2s"},
      -- {connections="5", duration="5s"},
      -- {connections="1", duration="2s"},
      -- {connections="10", duration="5s"},
      -- {connections="1k", duration="2s"},
      -- {connections="500", duration="5s"},
      -- {connections="1", duration="2s"},
      -- {connections="10", duration="5s"},
      -- {connections="1", duration="2s"},
      -- {connections="5", duration="5s"},
      -- {connections="1", duration="2s"},
      -- {connections="200", duration="5s"},
      -- {connections="4k", duration="2s"},
      -- {connections="5", duration="5s"},
      -- {connections="1", duration="2s"},
      -- {connections="5", duration="5s", threads=5},
      -- {connections="1", duration="10s", threads=1},
      -- {connections="5", duration="1s", threads=5},
      -- {connections="10", duration="1s", threads=2},
      -- {connections="1", duration="1s", threads=1},
      -- {connections="5", duration="1s", threads=5},
      -- {connections="10", duration="1s", threads=2},
      -- {connections="1", duration="1s", threads=1},
      -- {connections="5", duration="1s", threads=1},
      -- {connections="10", duration="1s", threads=1},
      -- {connections="1", duration="1s", threads=1},
      -- {connections="5", duration="1s", threads=1},
      -- {connections="10", duration="1s", threads=1},
      -- {connections="1", duration="1s", threads=1},
      -- {connections="5", duration="1s", threads=1},
      -- {connections="10", duration="1s", threads=1},
      -- {connections="1", duration="1s", threads=1},
      -- {connections="5", duration="1s", threads=1},
      -- {connections="10", duration="1s", threads=1},
      -- {connections="1", duration="1s", threads=1},
      -- {connections="5", duration="1s", threads=1},
      -- {connections="10", duration="1s", threads=1},
      -- {connections="1", duration="1s", threads=1},
      -- {connections="200", duration="10s", threads=1},
      -- {connections="8", duration="1m", threads=1},
   }
}

wrk.method = "POST"


local dashbord = {
   {method="POST", path="/login", body="god", w=1},
   {method="POST", path="/login1", body="test", w=1},
   -- {method="POST", path="/login2", body="token", w=1},
}

function init()
   collectgarbage("setpause",100)
   -- local file = io.open("out.txt", "r")
   -- print(file:read("*a"))
   -- local ctx = {}
   -- for line in io.lines("out.txt") 
   -- do
   --    print(line)
   -- end


   -- setmetatable(args, cjson.array_mt)
   for i=1, #dashbord 
   do 
      for j=1, dashbord[i].w 
      do 
         table.insert(req, dashbord[i])
      end
   end

   requests  = 0
   responses = 0
   local msg = "thread %d created"
end

function request()
   local memory_increased = collectgarbage("count") 
   print("request:".. memory_increased)
   local headers = {}
   local index = math.random(1, #req)
   local q = req[index]
   headers['Content-Type'] = "application/x-www-form-urlencoded"
   -- headers['Host'] = "localhost"
   local s = wrk.format(q['method'], q['path'], headers, q['body'])
   -- print("8 + 9 = ", wrk.uuid(1))
   return s
end


function response(status, c)
   local memory_increased = collectgarbage("count") 
   print("response:".. memory_increased)
   c:set("bizError", 1);
   -- print("id==>".. c.id)
   -- print("req method=>".. c.method)
   -- print("req==> ".. c.request)
   if status ~=200 then
      print("resp==>".. c.response_body)
   end
   -- print("status==>"..c.resquest_header.Host)
   -- print("resp headers==>"..cjson.encode(c.resquest_header))
   -- c:set("cxw", {api="http", uid=100})

   -- print("c get =>".. cjson.encode(c:get("cxw")))
   -- local headers = {}
   -- local index = math.random(1, #req)
   -- local q = req[index]
   -- headers['Content-Type'] = "application/x-www-form-urlencoded"
   -- headers['Host'] = "localhost"
   
   -- -- print(string.len(request.path))
   -- if request.path == "/login1" then
   --    local s = wrk.format("POST", "/login1", headers, "good")
   --    print(request.path)
   --    return s, 200
   -- end

   return nil,500
end

